package Vista;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import GestionDeDatos.GestorBD;
import InfoCliente.UsuarioActivo;

public class Inicio {
	
	private JPanel mainPanel = new JPanel();
	
	private JLabel jlb_intro;
	private ButtonGroup jbg_group = new ButtonGroup();
	private JRadioButton jcb_horarios, jcb_amigos;
	private JButton jb_cuenta;
	private JScrollPane scrollPane;
	private JTable table;
	
	public Inicio() {
		initComponents();
	}

	private void initComponents() {
		mainPanel.setLayout(null);
		mainPanel.add(getJLabelIntro());
		jbg_group.add(getJRadioButtonHorarios());
		jbg_group.add(getJRadioButtonAmigos());
		mainPanel.add(getJRadioButtonHorarios());
		mainPanel.add(getJRadioButtonAmigos());
		mainPanel.add(getJButtonCuenta());
		mainPanel.add(getTablaScroll());
		mainPanel.setSize(930, 850);
		
		this.verHorariosActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        		 
	        		 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        		 String[][] data = {};
		        	 String[] col = {};
		        	 
	        		 try {
	        			 data = GestorBD.Gestor().ObtenerInscripciones(usuarioActivo).getRegisters(); 
			        	 col = GestorBD.Gestor().ObtenerInscripciones(usuarioActivo).getFields();
	        		 } catch (Exception exception) {

	        		 }
	        		 
	        		 DefaultTableModel dm = new DefaultTableModel(data, col);
				     table.setModel(dm);  

	         }
	      });
		
		this.verAmigosActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        	 
	        		 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        		 String[][] data = {};
		        	 String[] col = {};
		        	 
	        		 try {
	        			 data = GestorBD.Gestor().ObtenerAmistades(usuarioActivo).getRegisters(); 
	        			 col = GestorBD.Gestor().ObtenerAmistades(usuarioActivo).getFields();
	        		 } catch (Exception exception) {

	        		 }
	        		 
	        		 DefaultTableModel dm = new DefaultTableModel(data, col);
				     table.setModel(dm);  

	         }
	      });
		
	}

	private JLabel getJLabelIntro() {
		if (jlb_intro == null) {
			jlb_intro = new JLabel("Bienvenido a NerdsMap App!");
			Font newTextFieldFont=new Font(jlb_intro.getFont().getName(),jlb_intro.getFont().getStyle(),18);   
		    jlb_intro.setFont(newTextFieldFont);  
		    jlb_intro.setBounds(50, 20, 800, 50);
		}

		return jlb_intro;
	}
	
	private JRadioButton getJRadioButtonHorarios() {
		if (jcb_horarios == null) {
			jcb_horarios = new JRadioButton();
			jcb_horarios.setText("Ver mis horarios");
			jcb_horarios.setSelected(true);
			jcb_horarios.setMargin(new Insets(1, 5, 1, 5));
			jcb_horarios.setBounds(50, 100, 200, 40); 
		}
		return jcb_horarios;
	}
	
	private JRadioButton getJRadioButtonAmigos() {
		if (jcb_amigos == null) {
			jcb_amigos = new JRadioButton();
			jcb_amigos.setText("Ver mis amigos");
			jcb_amigos.setMargin(new Insets(1, 5, 1, 5));
			jcb_amigos.setBounds(300, 100, 200, 40); 
		}
		return jcb_amigos;
	}
	
	private JButton getJButtonCuenta() {
		if (jb_cuenta == null) {
			jb_cuenta = new JButton();
			jb_cuenta.setText("Eliminar Cuenta");
			jb_cuenta.setMargin(new Insets(1, 5, 1, 5));
			jb_cuenta.setBounds(325, 410, 150, 40); 
		}
		return jb_cuenta;
	}
	
	private JScrollPane getTablaScroll() {

		String[][] data = {};
		String[] col = {};
		table = new JTable(data,col);
		JTableHeader header = table.getTableHeader();
		header.setBackground(Color.yellow);
		scrollPane = new JScrollPane(table);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		scrollPane.setBounds(20,150,500,250);
		  
		return scrollPane;
	}
	
	public JTable getTable2() {		
		return table;
	}
	
	protected boolean getAmigosIsSelected() {
		return jcb_amigos.isSelected();
	}
	
	public void EliminarCuentaActionListener(ActionListener listener) {
	    jb_cuenta.addActionListener(listener); 
	}
	
	public void verHorariosActionListener(ActionListener listener) {
	    jcb_horarios.addActionListener(listener); 
	}
	
	public void verAmigosActionListener(ActionListener listener) {
	    jcb_amigos.addActionListener(listener); 
	}
	
	public JComponent getMainComponent() {
	      return mainPanel;
	}

}
